package com.hj.authority.service.impl;

import cn.hutool.core.util.IdUtil;
import com.hj.authority.domain.constant.AuthorityConstant;
import com.hj.authority.domain.dto.EmailSendDTO;
import com.hj.authority.domain.query.SysAccountLoginQuery;
import com.hj.authority.domain.vo.HjTokenVO;
import com.hj.authority.service.SysAuthService;
import com.hj.authority.service.SysCaptchaService;
import com.hj.email.EmailService;
import com.hj.exception.HjException;
import com.hj.exception.HjExceptionEnum;
import com.hj.redis.RedisKeyConstant;
import com.hj.security.cache.TokenStoreCache;
import com.hj.security.user.HjUserDetails;
import com.hj.security.utils.TokenUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

/**
 * @author hj
 * @Date 2023/4/27
 * @Description: 权限认证服务
 */
@Service
@AllArgsConstructor
public class SysAuthServiceImpl implements SysAuthService {

    private final SysCaptchaService sysCaptchaService;
    private final TokenStoreCache tokenStoreCache;
    private final AuthenticationManager authenticationManager;
    private EmailService emailService;


    @Override
    public HjTokenVO loginByAccount(SysAccountLoginQuery login) {
        // 验证码效验
        boolean flag = sysCaptchaService.validate(login.getKey(), login.getCaptcha());
        if (!flag) {
            throw new HjException(HjExceptionEnum.CAPTCHA_ERR);
        }
        Authentication authentication;
        try {
            // 用户认证
            authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword()));
        } catch (BadCredentialsException e) {
            throw new HjException(HjExceptionEnum.ACCOUNT_PARAM_ERR);
        }
        // 用户信息
        HjUserDetails user = (HjUserDetails) authentication.getPrincipal();
        // 生成 accessToken
        String accessToken = TokenUtils.generator();
        // 保存用户信息到缓存
        tokenStoreCache.saveUser(accessToken, user);
        return new HjTokenVO(accessToken);
    }


    @Override
    public void logout(String accessToken) {
        // 用户信息
        HjUserDetails user = tokenStoreCache.getUser(accessToken);
        // todo 保存一些用户的登录信息
        // 删除用户信息
        tokenStoreCache.deleteUser(accessToken);
    }

    @Override
    public String email(EmailSendDTO dto) {
        String key = IdUtil.fastSimpleUUID();
        String redisPrefix = RedisKeyConstant.SYS_EMAIL;
        Boolean aBoolean = emailService.sendEmailCode(dto.getEmail(), AuthorityConstant.EMAIL_SUBJECT_ONE, redisPrefix + key);
        if (aBoolean) {
            return key;
        }
        throw new HjException(HjExceptionEnum.EMAIL_ERR);
    }


}
